#include <WiFi.h>
#include "movecontrol.h"
#include "moveserver.h"
#include "html.h"

// WiFi配置
const char* ssid = "忘记密码";
const char* password = "Sun@5460525";

AsyncWebServer server(80);
volatile int g_baseSpeed = 100;

void setup() {
    setupPWM();
    delay(100); // 等待硬件稳定
    Serial.begin(115200); // 添加串口初始化
    stopMotors();

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    
    // 打印IP地址到串口
    Serial.print("ESP32 IP Address: http://");
    Serial.println(WiFi.localIP());
    
    // Web服务器路由
    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send_P(200, "text/html", index_html);
    });

    // 提供控制麦克纳姆轮服务
    moveServer();

    server.on("/speed", HTTP_GET, [](AsyncWebServerRequest *request){
        if(request->hasParam("value")){
          int speed = request->getParam("value")->value().toInt();
          g_baseSpeed = map(speed, 0, 100, 66, 255);
          Serial.printf("speed is %d\n",g_baseSpeed);
        }
        freeSpeed();
        request->send(200);
    });

    server.begin();
}

void loop() {
    Serial.printf("speed is %d\n",g_baseSpeed);
    sleep(3);
}

